package org.example;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.*;
import org.junit.jupiter.api.Test;

/**
 * @link <a href="https://github.com/wechatpay-apiv3/wechatpay-java/blob/main/service/src/example/java/com/wechat/pay/java/service/partnerpayments/jsapi/JsapiServiceExample.java">示例</a>
 */
public class JsapiServiceExtensionExample {
    /** 商户号 */
    public static String merchantId = "1593191541";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "/Users/tanglonglong/idea_project/cloud-test/web-springboot/web-wx/src/main/resources/wx/apiclient_key.pem";
    /** 商户证书序列号 */
    public static String merchantSerialNumber = "73AAF1F8897D4ECE9C6A468D2621410D2BADAF2A";
    /** 商户APIV3密钥 */
    public static String apiV3Key = "77be67fe81c437f7d95e2bebd2082cb5";

    public static JsapiServiceExtension service;

    @Test
    public void test() {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();
        // 初始化服务
        service = new JsapiServiceExtension.Builder()
                        .config(config)
                        .signType("RSA") // 不填默认为RSA
                        .build();
        try {
            // ... 调用接口
            PrepayWithRequestPaymentResponse response = onlinePrepayWithRequestPayment();
            System.out.println("response: " + response);
        } catch (HttpException e) { // 发送HTTP请求失败
            e.printStackTrace();
            // 调用e.getHttpRequest()获取请求打印日志或上报监控，更多方法见HttpException定义
        } catch (ServiceException e) { // 服务返回状态小于200或大于等于300，例如500
            e.printStackTrace();
            // 调用e.getResponseBody()获取返回体打印日志或上报监控，更多方法见ServiceException定义
        } catch (MalformedMessageException e) { // 服务返回成功，返回体类型不合法，或者解析返回体失败
            e.printStackTrace();
            // 调用e.getMessage()获取信息打印日志或上报监控，更多方法见MalformedMessageException定义
        }
    }

    public static PrepayWithRequestPaymentResponse testPrepayWithRequestPayment() {
        // 商户申请的公众号对应的appid，由微信支付生成，可在公众号后台查看
        PrepayRequest request = new PrepayRequest();
        String requestPaymentAppid = "wx4daf5b46706f06e5";
        request.setSpAppid("wx8aebb211c7179b9d");
        request.setSpMchid("1593191541");
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setOutTradeNo("No000001");
        request.setSubAppid(requestPaymentAppid);
        request.setSubMchid("1612082591");
        request.setNotifyUrl("https://www.weixin.qq.com/wxpay/pay.php");
        request.setDescription("小程序充值费用");
        Payer payer = new Payer();
        payer.setSubOpenid("oT5Dk5JvwCYKXqxnrpIqcjWMDiQs");
        request.setPayer(payer);
        // 调用接口
        return service.prepayWithRequestPayment(request, requestPaymentAppid);
    }

    public static PrepayWithRequestPaymentResponse onlinePrepayWithRequestPayment() {
        // 商户申请的公众号对应的appid，由微信支付生成，可在公众号后台查看
        PrepayRequest request = new PrepayRequest();
        String requestPaymentAppid = "wxc1cd1769fb0a22e3";
        request.setSpAppid("wx8aebb211c7179b9d");
        request.setSpMchid("1593191541");
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setOutTradeNo("No00000102");
        request.setSubAppid(requestPaymentAppid);
        request.setSubMchid("1615555941");
        request.setNotifyUrl("https://www.weixin.qq.com/wxpay/pay.php");
        request.setDescription("小程序充值费用");
        Payer payer = new Payer();
        payer.setSubOpenid("oNFPC5ZTZPmjiCjBqD5cet-cToRA");
        request.setPayer(payer);
        // 调用接口
        return service.prepayWithRequestPayment(request, requestPaymentAppid);
    }

    /** JSAPI支付下单，并返回JSAPI调起支付数据 */
    public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {
        // 商户申请的公众号对应的appid，由微信支付生成，可在公众号后台查看
        PrepayRequest request = new PrepayRequest();
        String requestPaymentAppid = "wx8aebb211c7179b9d";
        request.setSpAppid("wx8aebb211c7179b9d");
        request.setSpMchid("1593191541");
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setOutTradeNo("No000001");
        request.setSubAppid("wx49a3d39539892904");
        request.setSubMchid("1615555941");
        request.setNotifyUrl("https://www.weixin.qq.com/wxpay/pay.php");
        request.setDescription("小程序充值费用");
        Payer payer = new Payer();
        payer.setSubOpenid("o77ev5HCBzH0JuazEpcmGtdXxLeQ");
        request.setPayer(payer);
        // 调用接口
        return service.prepayWithRequestPayment(request, requestPaymentAppid);
    }

    public static PrepayWithRequestPaymentResponse tunHuiPrepayWithRequestPayment() {
        // 商户申请的公众号对应的appid，由微信支付生成，可在公众号后台查看
        PrepayRequest request = new PrepayRequest();
        String requestPaymentAppid = "wx51a932dd91c6afa0";
        request.setSpAppid("wx8aebb211c7179b9d");
        request.setSpMchid("1593191541");
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setOutTradeNo("No000001");
        request.setSubAppid(requestPaymentAppid);
        request.setSubMchid("1602410343");
        request.setNotifyUrl("https://www.weixin.qq.com/wxpay/pay.php");
        request.setDescription("小程序充值费用");
        Payer payer = new Payer();
        // liyilan
        payer.setSubOpenid("o3hkM47wgJ4juZsQQEH9I9NLDHr8");
        request.setPayer(payer);
        // 调用接口
        return service.prepayWithRequestPayment(request, requestPaymentAppid);
    }

    /** 关闭订单 */
    public static void closeOrder() {
        CloseOrderRequest request = new CloseOrderRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        // 调用接口
        service.closeOrder(request);
    }
    /**
     * 微信支付订单号查询订单
     */
    public static Transaction queryOrderById() {

        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        // 调用接口
        return service.queryOrderById(request);
    }
    /** 商户订单号查询订单 */
    public static Transaction queryOrderByOutTradeNo() {

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        // 调用接口
        return service.queryOrderByOutTradeNo(request);
    }
}
